package com.example.myapplication.model;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class RegisterTask extends AsyncTask<Void, Void, String> {
    private Context context;
    private String phoneNumber;
    private String password;

    public RegisterTask(Context context, String phoneNumber, String password) {
        this.context = context;
        this.phoneNumber = phoneNumber;
        this.password = password;
    }

    @Override
    protected String doInBackground(Void... voids) {
        try {
            URL url = new URL("http://39.107.92.86:5000/register");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);

            JSONObject postData = new JSONObject();
            try {
                postData.put("phone_number", phoneNumber);
                postData.put("password", password);
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }

            conn.setRequestProperty("Content-Type", "application/json");

            OutputStream out = new BufferedOutputStream(conn.getOutputStream());
            out.write(postData.toString().getBytes());
            out.flush();
            out.close();

            InputStream in;
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                in = conn.getInputStream();
            } else {
                in = conn.getErrorStream();
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            reader.close();
            conn.disconnect();

            return response.toString();
        } catch (IOException e) {
            Log.e("RegisterTask", "Error sending registration request", e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        if (result != null) {
            try {
                JSONObject responseJson = new JSONObject(result);
                boolean success = responseJson.getBoolean("success");
                if (success) {
                    Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "注册失败，手机号已存在", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                Log.e("RegisterTask", "Error parsing server response", e);
                Toast.makeText(context, "注册失败，服务器响应解析错误", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, "注册失败，无法连接到服务器", Toast.LENGTH_SHORT).show();
        }
    }
}
